package fzdy.core.common;

/**
 * Created by Administrator on 2017/3/27 0027.
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTool {
    public static final Calendar calendar = Calendar.getInstance();
    static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");

    private void initCalendar() {
        Date date = new Date();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
    }

    public DateTool() {
        this.initCalendar();
    }

    public Date getBeginTime() {
        calendar.add(5, 0);
        return calendar.getTime();
    }

    public Date getEndTime() {
        calendar.add(5, 1);
        return calendar.getTime();
    }

    public static long fromMonth(Date time) {
        Calendar nowDate = new GregorianCalendar();
        nowDate.setTime(time);
        nowDate.set(12, 0);
        nowDate.set(13, 0);
        nowDate.set(14, 0);
        nowDate.set(5, 1);
        Date date = nowDate.getTime();
        long begin = date.getTime();
        return begin;
    }

    public static long toMonth(Date time) {
        Calendar nowDate = new GregorianCalendar();
        nowDate.setTime(time);
        nowDate.set(12, 0);
        nowDate.set(13, 0);
        nowDate.set(14, 0);
        nowDate.set(5, 1);
        nowDate.add(2, 1);
        Date date = nowDate.getTime();
        long end = date.getTime();
        return end;
    }

    public static Long firstDayOfLastMonth() {
        Calendar cal_1 = Calendar.getInstance();
        cal_1.add(2, -1);
        cal_1.set(5, 1);
        String firstDay = ymd.format(cal_1.getTime());
        Long time = parseDateStr(firstDay);
        return time;
    }

    public static Long lastDayOfLastMonth() {
        Calendar c = Calendar.getInstance();
        c.add(2, 0);
        c.set(5, 1);
        String first = ymd.format(c.getTime());
        Long ime = parseDateStr(first);
        return ime;
    }

    public static String timeToStrYmd(Long time) {
        return ymd.format(new Date(time.longValue()));
    }

    public static Long parseDateStr(String date) {
        try {
            Date parse = ymd.parse(date);
            return Long.valueOf(parse.getTime());
        } catch (ParseException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Long time = firstDayOfLastMonth();
        System.out.println(time);
        Long last = lastDayOfLastMonth();
        System.out.print(last);
    }
}
